{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ShortPath import Floyd,inf\n",
    "import numpy as np \n",
    "import pandas as pd \n",
    "import networkx as nx \n",
    "from matplotlib import pyplot as plt "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def netflow(OD, T0, C, n=100, alpha=1, beta=.5):\n",
    "    m = len(T0)\n",
    "\n",
    "    # 初始化\n",
    "    T = np.array(T0)\n",
    "    Q = np.zeros_like(T0)\n",
    "\n",
    "    def updateQ(COD, P):\n",
    "        P = np.array(P)\n",
    "        for i in range(m):\n",
    "            for j in range(m):\n",
    "                path = P[i, j]\n",
    "                for k in range(len(path)-1):\n",
    "                    s, t = path[k], path[k+1]\n",
    "                    Q[s, t] += COD[i, j]\n",
    "\n",
    "    def updateT():\n",
    "        return T0*(1+alpha*((Q/C)**beta))\n",
    "\n",
    "    for i in range(n):\n",
    "        _, P = Floyd(T)\n",
    "        updateQ(OD/n, P)\n",
    "        T = updateT()\n",
    "\n",
    "    return Q, T, T/T0 - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def evalue(Q, T, J):\n",
    "        RT = T[(T != inf)]\n",
    "        QT = (Q*T).flatten()\n",
    "        QT = QT[~np.isnan(QT)]\n",
    "        J = J.flatten()\n",
    "        J = J[~np.isnan(J)]\n",
    "        return {\n",
    "            '平均道路通行时间': np.mean(RT),\n",
    "            '加权路网通行时耗': np.sum(QT),\n",
    "            '路网拥堵指数': np.mean(J)\n",
    "        }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def run():\n",
    "    Q, T, J = netflow(OD, T0, C)\n",
    "    print('Q=', Q)\n",
    "    print('J=', J)\n",
    "    eval0 = evalue(Q, T, J)\n",
    "    print('evalue=', eval0)\n",
    "    return Q, T, J"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_graph(cmap=plt.cm.RdYlGn):\n",
    "    g = nx.Graph()\n",
    "    colors = []\n",
    "    for i in range(m):\n",
    "        for j in range(m):\n",
    "            if T0[i,j] < inf:\n",
    "                g.add_edge(i,j,weight=T0[i,j])\n",
    "                colors.append(-np.log(J[i,j]))\n",
    "    options = {\n",
    "        \"node_color\": \"#A0CBE2\",\n",
    "        \"edge_color\": colors,\n",
    "        \"width\": 4,\n",
    "        \"edge_cmap\": cmap,\n",
    "        \"with_labels\": True\n",
    "    }\n",
    "    nx.draw_kamada_kawai(g,**options)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "增加道路1-4之前:\nQ= [[  0. 510. 690.   0.]\n [  0.   0.   0. 310.]\n [  0.   0.   0. 890.]\n [  0.   0.   0.   0.]]\nJ= [[       nan 1.00995049 2.62678511        nan]\n [       nan        nan        nan 0.78740079]\n [       nan        nan        nan 2.98328678]\n [       nan        nan        nan        nan]]\nevalue= {'平均道路通行时间': 3.801193611955694, '加权路网通行时耗': 9205.944948398836, '路网拥堵指数': 1.8518557916463467}\n增加道路1-4之后：\nQ= [[  0. 240. 300. 660.]\n [  0.   0.   0.  40.]\n [  0.   0.   0. 500.]\n [  0.   0.   0.   0.]]\nJ= [[       nan 0.69282032 1.73205081 2.96647939]\n [       nan        nan        nan 0.28284271]\n [       nan        nan        nan 2.23606798]\n [       nan        nan        nan        nan]]\nevalue= {'平均道路通行时间': 3.5738327896660804, '加权路网通行时耗': 7279.645003961635, '路网拥堵指数': 1.5820522430818205}\n"
    },
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "module 'matplotlib.cm' has no attribute 'PiYlGn'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-7b2be1b639b0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     31\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msubplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m122\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     32\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtitle\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'after join the network of community:'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 33\u001b[1;33m \u001b[0mdraw_graph\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcm\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPiYlGn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'matplotlib.cm' has no attribute 'PiYlGn'"
     ]
    }
   ],
   "source": [
    "T0 = np.array([\n",
    "        [inf, 2, 1, inf],\n",
    "        [inf, inf, inf, 2],\n",
    "        [inf, inf, inf, 1],\n",
    "        [inf, inf, inf, inf]\n",
    "    ])\n",
    "OD = np.array([\n",
    "        [0, 200, 0, 1000],\n",
    "        [0, 0, 0, 0],\n",
    "        [0, 0, 0, 200],\n",
    "        [0, 0, 0, 0]\n",
    "    ])\n",
    "C = np.array([\n",
    "        [1, 500, 100, 1],\n",
    "        [1, 1, 1, 500],\n",
    "        [1, 1, 1, 100],\n",
    "        [1, 1, 1, 1]\n",
    "    ])\n",
    "m = len(T0)\n",
    "\n",
    "print('增加道路1-4之前:')\n",
    "Q, T, J = run()\n",
    "plt.subplot(121)\n",
    "plt.title('before join the network of community:')\n",
    "draw_graph()\n",
    "\n",
    "print('增加道路1-4之后：')\n",
    "T0[0, 3] = 1.5\n",
    "C[0, 3] = 75\n",
    "Q, T, J = run()\n",
    "plt.subplot(122)\n",
    "plt.title('after join the network of community:')\n",
    "draw_graph(plt.cm.YlGn)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python36864bit3f53e4684f024fc68f038ddcedf7a612",
   "display_name": "Python 3.6.8 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}